package registry

import (
	"fmt"

	"gitee.com/mkwchecking/sr-core/yal/ymap"
	"github.com/go-kratos/kratos/v2/registry"
)

type (
	RegistryFac  func(Config) (registry.Registrar, error)
	DiscoveryFac func(Config) (registry.Discovery, error)
)

var (
	_registryContainer  = ymap.New[Engine, RegistryFac]()
	_discoveryContainer = ymap.New[Engine, DiscoveryFac]()
)

func RegisterRegistry(eng Engine, fac RegistryFac) {
	_registryContainer.Register(eng, fac)
}
func UnregisterRegistry(eng Engine) {
	_registryContainer.Unregister(eng)
}
func GetRegistry(eng Engine) (RegistryFac, error) {
	fac, ok := _registryContainer.Get(eng)
	if !ok {
		return nil, fmt.Errorf("engine %s not found", eng)
	}
	return fac, nil
}

func RegisterDiscovery(eng Engine, fac DiscoveryFac) {
	_discoveryContainer.Register(eng, fac)
}
func UnregisterDiscovery(eng Engine) {
	_discoveryContainer.Unregister(eng)
}
func GetDiscovery(eng Engine) (DiscoveryFac, error) {
	fac, ok := _discoveryContainer.Get(eng)
	if !ok {
		return nil, fmt.Errorf("engine %s not found", eng)
	}
	return fac, nil
}
